package com.cuit.cuiter.service;

import cn.hutool.crypto.digest.DigestUtil;
import com.cuit.cuiter.dao.UserRepository;
import com.cuit.user.User;
import com.cuit.user.UserCode;
import com.cuit.user.Vo.Login;
import com.cuit.user.Vo.Register;
import com.cuit.user.request.LoginUserRequest;
import common.result.CodeEnum;
import common.result.ResponseResult;
import common.utils.RedisUtils;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class LoginRegisterService {
    @Autowired
    UserRepository userRepository;
    @Autowired
    RedisUtils redisUtils;

    public List<User> findAll() {
        return userRepository.findAll();
    }

    /**
     * 用户登录
     *
     * @param loginUserRequest
     * @return
     */
    public ResponseResult login(LoginUserRequest loginUserRequest) {
        User user = userRepository.findById(loginUserRequest.getMailaddress()).orElse(null);
        if (user == null) {
            return new ResponseResult(UserCode.LOGIN_FAIL.getCode(), UserCode.LOGIN_FAIL.getMessage());
        }

        String md5Hex1 = DigestUtil.md5Hex(loginUserRequest.getPassword());
        boolean verify = StringUtils.pathEquals(md5Hex1, user.getPassword());
//        用户名密码正确
        if (verify) {
            //签发token
            long now = System.currentTimeMillis();//当前时间
            long exp = now + 1000 * 3600 * 10;//过期时间为1分钟
            JwtBuilder builder = Jwts.builder().setId(loginUserRequest.getMailaddress()).setSubject(user.toString())
                    .setIssuedAt(new Date())//设置签发时间
                    .setExpiration(new Date(exp))//设置过期时间
                    .signWith(SignatureAlgorithm.HS256, "xiaocai");//设置签名秘钥
            String token = builder.compact();
            Login login = new Login();
            login.setToken(token);
            login.setUser(user);
            return new ResponseResult(CodeEnum.SUCCESS.getCode(), login);
        } else {
            return new ResponseResult(UserCode.LOGIN_FAIL.getCode(), "登录失败,密码错误");
        }

    }

    /**
     * 注册
     *
     * @param user
     * @return
     */
    public ResponseResult register(Register register) {
        User user = new User();
        BeanUtils.copyProperties(register, user);
        String code = redisUtils.getCheckCode(user.getMailaddress());
        if (code == null) {
            return new ResponseResult(CodeEnum.CHECK_CODE_ERROR.getCode(), CodeEnum.CHECK_CODE_ERROR.getMessage());
        }

        if (StringUtils.pathEquals(code, register.getCheckCode())) {
            user.setPassword(DigestUtil.md5Hex(user.getPassword()));
            userRepository.save(user);
            System.out.println("保存成功");
            return new ResponseResult(CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMessage());
        }
        return new ResponseResult(CodeEnum.CHECK_CODE_ERROR.getCode(), CodeEnum.CHECK_CODE_ERROR.getMessage());
    }
}
